﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;

namespace Utils
{
    public enum DateTimePattern
    {
        FullDateTimePattern,
        LongDatePattern,
        MonthDayPattern,
        ShortDatePattern,
        SortableDateTimePattern,
        UniversalSortableDateTimePattern,
        YearMonthPattern
    }

    public static class DateTimeLocation
    {
        private static DateTimeFormatInfo localizationFormat;

        static DateTimeLocation()
        {
            localizationFormat = CultureInfo.CurrentCulture.DateTimeFormat;
        }

        public static DateTime FromStringToDateTime(string item)
        {
            try
            {
                DateTime dateTime = DateTime.Parse(item);
                return dateTime;
            }
            catch (Exception ex) { throw new Exception(ex.Message); }
        }

        public static DateTime FromStringToDateTime(string item, DateTimePattern pattern)
        {
            DateTime dateTime;
            try
            {
                switch (pattern)
                {
                    case DateTimePattern.FullDateTimePattern:
                        dateTime = DateTime.ParseExact(item, localizationFormat.FullDateTimePattern, CultureInfo.InvariantCulture);
                        break;
                    case DateTimePattern.LongDatePattern:
                        dateTime = DateTime.ParseExact(item, localizationFormat.LongDatePattern, CultureInfo.InvariantCulture);
                        break;
                    case DateTimePattern.MonthDayPattern:
                        dateTime = DateTime.ParseExact(item, localizationFormat.MonthDayPattern, CultureInfo.InvariantCulture);
                        break;
                    case DateTimePattern.ShortDatePattern:
                        dateTime = DateTime.ParseExact(item, localizationFormat.ShortDatePattern, CultureInfo.InvariantCulture);
                        break;
                    case DateTimePattern.SortableDateTimePattern:
                        dateTime = DateTime.ParseExact(item, localizationFormat.SortableDateTimePattern, CultureInfo.InvariantCulture);
                        break;
                    case DateTimePattern.UniversalSortableDateTimePattern:
                        dateTime = DateTime.ParseExact(item, localizationFormat.UniversalSortableDateTimePattern, CultureInfo.InvariantCulture);
                        break;
                    case DateTimePattern.YearMonthPattern:
                        dateTime = DateTime.ParseExact(item, localizationFormat.YearMonthPattern, CultureInfo.InvariantCulture);
                        break;
                    default:
                        dateTime = DateTime.ParseExact(item, localizationFormat.FullDateTimePattern, CultureInfo.InvariantCulture);
                        break;
                }
            }
            catch (Exception ex) { throw new Exception(ex.Message); }

            return dateTime;
        }

        public static DateTime FromStringToDateTime(string item, string localization)
        {
            try
            {
                DateTime dateTime = DateTime.Parse(item, CultureInfo.CreateSpecificCulture(localization));
                return dateTime;
            }
            catch (Exception ex) { throw new Exception(ex.Message); }
        }

        public static DateTime FromStringToDateTime(string item, string pattern, string localization)
        {
            try
            {
                DateTime dateTime = DateTime.ParseExact(item, pattern, CultureInfo.CreateSpecificCulture(localization));
                return dateTime;
            }
            catch (Exception ex) { throw new Exception(ex.Message); }

        }
    }
}